package ltd.cheers.mall.service.impl;

import ltd.cheers.mall.common.Constants;
import ltd.cheers.mall.common.ServiceResultEnum;
import ltd.cheers.mall.controller.vo.CheersMallShoppingCartItemVO;
import ltd.cheers.mall.dao.CheersMallGoodsMapper;
import ltd.cheers.mall.dao.CheersMallShoppingCartItemMapper;
import ltd.cheers.mall.entity.CheersMallGoods;
import ltd.cheers.mall.entity.CheersMallShoppingCartItem;
import ltd.cheers.mall.service.CheersMallShoppingCartService;
import ltd.cheers.mall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: LiHuanYang
 * @date: 2020/3/25 下午 04:03
 */

@Service
public class CheersMallShoppingCartServiceImpl implements CheersMallShoppingCartService {

    @Autowired
    private CheersMallShoppingCartItemMapper cheersMallShoppingCartItemMapper;

    @Autowired
    private CheersMallGoodsMapper cheersMallGoodsMapper;

    //todo 修改session中购物项数量

    @Override
    public String saveCheersMallCartItem(CheersMallShoppingCartItem cheersMallShoppingCartItem) {
        CheersMallShoppingCartItem temp = cheersMallShoppingCartItemMapper.selectByUserIdAndGoodsId(cheersMallShoppingCartItem.getUserId(), cheersMallShoppingCartItem.getGoodsId());
        if (temp != null) {
            //已存在则修改该记录
            //todo count = tempCount + 1
            temp.setGoodsCount(cheersMallShoppingCartItem.getGoodsCount());
            return updateCheersMallCartItem(temp);
        }
        CheersMallGoods cheersMallGoods = cheersMallGoodsMapper.selectByPrimaryKey(cheersMallShoppingCartItem.getGoodsId());
        //商品为空
        if (cheersMallGoods == null) {
            return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
        }
        int totalItem = cheersMallShoppingCartItemMapper.selectCountByUserId(cheersMallShoppingCartItem.getUserId());
        //超出最大数量
        if (totalItem > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //保存记录
        if (cheersMallShoppingCartItemMapper.insertSelective(cheersMallShoppingCartItem) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateCheersMallCartItem(CheersMallShoppingCartItem cheersMallShoppingCartItem) {
        CheersMallShoppingCartItem cheersMallShoppingCartItemUpdate = cheersMallShoppingCartItemMapper.selectByPrimaryKey(cheersMallShoppingCartItem.getCartItemId());
        if (cheersMallShoppingCartItemUpdate == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        //超出最大数量
        if (cheersMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //todo 数量相同不会进行修改
        //todo userId不同不能修改
        cheersMallShoppingCartItemUpdate.setGoodsCount(cheersMallShoppingCartItem.getGoodsCount());
        cheersMallShoppingCartItemUpdate.setUpdateTime(new Date());
        //修改记录
        if (cheersMallShoppingCartItemMapper.updateByPrimaryKeySelective(cheersMallShoppingCartItemUpdate) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public CheersMallShoppingCartItem getCheersMallCartItemById(Long cheersMallShoppingCartItemId) {
        return cheersMallShoppingCartItemMapper.selectByPrimaryKey(cheersMallShoppingCartItemId);
    }

    @Override
    public Boolean deleteById(Long cheersMallShoppingCartItemId) {
        //todo userId不同不能删除
        return cheersMallShoppingCartItemMapper.deleteByPrimaryKey(cheersMallShoppingCartItemId) > 0;
    }

    @Override
    public List<CheersMallShoppingCartItemVO> getMyShoppingCartItems(Long cheersMallUserId) {
        List<CheersMallShoppingCartItemVO> cheersMallShoppingCartItemVOS = new ArrayList<>();
        List<CheersMallShoppingCartItem> cheersMallShoppingCartItems = cheersMallShoppingCartItemMapper.selectByUserId(cheersMallUserId, Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER);
        if (!CollectionUtils.isEmpty(cheersMallShoppingCartItems)) {
            //查询商品信息并做数据转换
            List<Long> cheersMallGoodsIds = cheersMallShoppingCartItems.stream().map(CheersMallShoppingCartItem::getGoodsId).collect(Collectors.toList());
            List<CheersMallGoods> cheersMallGoods = cheersMallGoodsMapper.selectByPrimaryKeys(cheersMallGoodsIds);
            Map<Long, CheersMallGoods> cheersMallGoodsMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(cheersMallGoods)) {
                cheersMallGoodsMap = cheersMallGoods.stream().collect(Collectors.toMap(CheersMallGoods::getGoodsId, Function.identity(), (entity1, entity2) -> entity1));
            }
            for (CheersMallShoppingCartItem cheersMallShoppingCartItem : cheersMallShoppingCartItems) {
                CheersMallShoppingCartItemVO cheersMallShoppingCartItemVO = new CheersMallShoppingCartItemVO();
                BeanUtil.copyProperties(cheersMallShoppingCartItem, cheersMallShoppingCartItemVO);
                if (cheersMallGoodsMap.containsKey(cheersMallShoppingCartItem.getGoodsId())) {
                    CheersMallGoods cheersMallGoodsTemp = cheersMallGoodsMap.get(cheersMallShoppingCartItem.getGoodsId());
                    cheersMallShoppingCartItemVO.setGoodsCoverImg(cheersMallGoodsTemp.getGoodsCoverImg());
                    String goodsName = cheersMallGoodsTemp.getGoodsName();
                    // 字符串过长导致文字超出的问题
                    if (goodsName.length() > 28) {
                        goodsName = goodsName.substring(0, 28) + "...";
                    }
                    cheersMallShoppingCartItemVO.setGoodsName(goodsName);
                    cheersMallShoppingCartItemVO.setSellingPrice(cheersMallGoodsTemp.getSellingPrice());
                    cheersMallShoppingCartItemVOS.add(cheersMallShoppingCartItemVO);
                }
            }
        }
        return cheersMallShoppingCartItemVOS;
    }
}
